const app = getApp();
import { zg99Request } from "/utils/requests";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    options: {
      s1: 'y',
      xiangmu: "sk",
      lx: "sk",
      selectNo: 'y',
    },
    yl: 1,
    lx: 'sk',
    myPhone: '',
    cxlists: [],
    chaxunjg: 0,
    options: [],
    page: 1,
    zcpages: 1,
    onReachBottomNum: 0,
    grdata: [], //查询个人数据
    qxdata: [], //取消时的数据
    showContact: true,
    suspendShow: true,
    showIcon: true,
    bgcolor: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    await app.methods.getPhoneNumber()
    if (options.data != undefined) {
      var t = JSON.parse(options.data);
    } else {
      var t = options;
    }
    if (options.yl != undefined) {
      if (options.yl.length > 0) {
        this.setData({
          yl: Number(options.yl)
        })
      }
    }
    this.setData({
      options: t,
      onReachBottomNum: 0,
      myPhone: t.myPhone
    })
    let grdata = {
      phone: this.data.myPhone,
      flag: true,
      tabnum: 3,
      limits: 60
    }
    this.setData({
      grdata: grdata
    })
    this.setData(this.getUser(grdata))
  },
  //查询个人收藏列表
  async getUser(data) {
    return new Promise((resolve) => {
      this.data.myPhone != '' ? zg99Request({
        url: '/index/chaxun/getfzinfo?actid=55246&callback=&',
        method: 'GET',
        dataType: 'jsonp',
        data: {
          ...data,
          time: Date.parse(new Date()) / 1e3
        },
      }).then(res => {
        let newStr = app.methods.getAPIReturn(res);
        if (newStr.msg != '暂无数据') {
          var lists = newStr.lists
          var newObj = [];
          newObj = lists
          this.setData({
            table: true,
            chaxunjg: newStr.total,
            cxlists: this.data.cxlists.concat(newObj),
          })
          xhs.hideLoading()
          xhs.showToast({
            title: '数据查询成功!',
            icon: 'success',
            duration: 2000
          })
        } else {
          this.setData({
            table: true,
            chaxunjg: 0,
            cxlists: [],
          })
          xhs.hideLoading()
          xhs.showToast({
            title: '你暂时还没有收藏任何职位，快去查询心仪的职位吧！!',
            icon: 'none',
            duration: 2000
          })
        }
        resolve()
      }).catch(err => {
        console.log(err)
        resolve()
      }) : this.setData({
        isShowAuth: !0,
      })
    })
  },
  // 去掉收藏信息
  async deleteCollect(e) {
    xhs.showLoading({
      title: '取消收藏',
      icon: 'none',
      duration: 1000,
    })
   
    // 1000后执行
    let str12 = e.currentTarget.dataset.str12,
      index = e.currentTarget.dataset.index;
    let data = {
      phone: this.data.myPhone,
      str12: str12,
      flag: true,
      tabnum: 3,
    }
    this.data.cxlists.splice(index, 1)
    //只从当前数据中移除，不重新走接口查询新的收藏列表

    return new Promise((resolve) => {
      zg99Request({
        url: '/index/chaxun/getfzinfo?actid=55246&callback=&',
        method: 'GET',
        dataType: 'jsonp',
        data: {
          ...data,
          time: Date.parse(new Date()) / 1e3
        },
      }).then(res => {
        let newStr = app.methods.getAPIReturn(res);
        var qxdata = {
          flag: false,
          tabnum: 3,
          str12: str12,
          fzid: newStr.lists[0].id
        }
        this.setData({
          qxdata: qxdata
        })
        this.updatefz(qxdata)
        // 返回id
        resolve()
      }).catch(err => {
        console.log(err)
        resolve()
      })
    })
    
    
  },
  // 更新辅助表信息
  updatefz() {
    let e = this;
    zg99Request({
      url: '/index/chaxun/updatefz?actid=55246&callback=&',
      method: 'GET',
      dataType: 'jsonp',
      data: {
        ...this.data.qxdata,
        time: Date.parse(new Date()) / 1e3
      }
    }).then(res => {
      let newStr = app.methods.getAPIReturn(res);
      this.setData({
        chaxunjg: this.data.chaxunjg - 1
      })
      xhs.hideLoading()
      xhs.showToast({
        title: '取消收藏!',
        icon: 'success',
        duration: 1000
      })
      setTimeout(() => {
        //this的指代不再是窗口，settimeout方法，需要更换指代
        this.setData({
          cxlists: this.data.cxlists
        })
      },200)
      if(this.data.chaxunjg==0){
        this.setData({
          table:false,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // 回到顶部
  toTop() {
    xhs.pageScrollTo({
      scrollTop: 0
  })
},
  //去往查询
  goToCX: function () {
    var options = {
      lx: this.data.options.lx,
      actid: this.data.options.actid,
      xiangmu: this.data.options.xiangmu
    }
    xhs.navigateBack({})
  },
  // 展开和收起
  toggle: function(e){
      const { index } = e.currentTarget.dataset;
      // console.log(index)
      let { cxlists } = this.data;
      cxlists[index].toggle = !cxlists[index].toggle
      this.setData({
          cxlists
      })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 用户点击右上角分享
   */
  /*  onShareAppMessage: function () {

   } */
})